package com.fafa.swing.base;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: CalculatorDemo
 * @date Date : 2019年11月24日 16:47
 */
public class CalculatorDemo extends JFrame {
    public static void main(String[] args) {
        CalculatorDemo calculatorDemo = new CalculatorDemo();
        calculatorDemo.setVisible(true);
    }

    public CalculatorDemo(){
        setTitle("计算器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(300,300,250,200);
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0,0));
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        setContentPane(contentPane);

        // 添加计算器显示栏位
        JPanel jPanel1 = new JPanel();
        contentPane.add(jPanel1, BorderLayout.NORTH);
        JTextField jTextField = new JTextField();
        jTextField.setHorizontalAlignment(SwingConstants.RIGHT);
        jTextField.setColumns(18);
        jPanel1.add(jTextField);

        JPanel jPanel2 = new JPanel();
        contentPane.add(jPanel2,BorderLayout.CENTER);
        jPanel2.setLayout(new GridLayout(4,4,5,5));
        makeButton(jPanel2,"7");
        makeButton(jPanel2,"8");
        makeButton(jPanel2,"9");
        makeButton(jPanel2,"+");
        makeButton(jPanel2,"4");
        makeButton(jPanel2,"5");
        makeButton(jPanel2,"6");
        makeButton(jPanel2,"-");
        makeButton(jPanel2,"3");
        makeButton(jPanel2,"2");
        makeButton(jPanel2,"1");
        makeButton(jPanel2,"*");
        makeButton(jPanel2,"0");
        makeButton(jPanel2,".");
        makeButton(jPanel2,"=");
        makeButton(jPanel2,"/");

    }

    public void makeButton(JPanel jPanel,String text){
        JButton jButton = new JButton(text);
        jPanel.add(jButton);
    }
}
